package com.axend.aerosense.room.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.bean.FunctionRoomEnum;
import com.axend.aerosense.common.bean.VipFunSetBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.ui.CustomSettingItemView;
import com.axend.aerosense.common.ui.CustomTipsDialog;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.room.BR;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomActivitySettingFunctionBinding;
import com.axend.aerosense.room.databinding.RoomActivityVipFunBinding;
import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.entity.VipFunAboutBean;
import com.axend.aerosense.room.listener.OnBackCallback;
import com.axend.aerosense.room.listener.OnSwitchChageListener;
import com.axend.aerosense.room.ui.fragment.RoomSettingFunctionFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomBathroomUseAnalysisFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomNightNoBackFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomNightUpAnalysisFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomOutNoBackFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomSedentaryAnalysisFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomSleepQualityAnalysisFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomSleepTimeoutFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomStandingStillFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomToiletDetentionFragment;
import com.axend.aerosense.room.ui.fragment.vipfun.RoomWorkRestFragment;
import com.axend.aerosense.room.viewmodel.RoomVipFunViewModel;
import com.blankj.utilcode.util.LogUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.io.Serializable;

public class RoomSettingFunctionActivity
        extends MvvmBaseActivity<RoomActivitySettingFunctionBinding, RoomVipFunViewModel> {

    @Override
    protected RoomVipFunViewModel getViewModel() {
        return new ViewModelProvider(this).get(RoomVipFunViewModel.class);
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.room_activity_setting_function;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);

        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        setSupportActionBar(viewDataBinding.roomToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());

        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        FunctionRoomEnum functionRoomEnum =
                (FunctionRoomEnum) intent.getSerializableExtra(RoomSettingFunctionFragment.KEY_FUNCTION_ENUM);

        RoomSettingFunctionFragment functionFragment =
                RoomSettingFunctionFragment.getInstance(intent.getExtras());
        FragmentUtil.add(
                this,
                getContentId(),
                functionFragment,
                null,
                false
        );

        //关于此vip功能介绍
        viewDataBinding.roomFunAbout.setOnClickListener(v -> {
            Intent i = new Intent(this, RoomVipFunAboutActivity.class);
            i.putExtra(RoomVipFunAboutActivity.FLAG_CONTENT, functionRoomEnum);
            startActivity(i);
        });
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = () -> {
            FragmentManager manager = getSupportFragmentManager();
            if (manager != null) {
                Fragment currFrag = manager.findFragmentById(getContentId());
                currFrag.onResume();
            }
        };

        return result;
    }

    public int getContentId() {
        return viewDataBinding.roomContent.getId();
    }

    public void setTitle(String title) {
        viewDataBinding.roomTitle.setText(title);
    }

    private OnBackCallback getOnBackCallback(){
        Fragment currentFrag = getSupportFragmentManager().findFragmentById(getContentId());
        if (currentFrag instanceof RoomSettingFunctionFragment) {
            return (OnBackCallback) currentFrag;
        }

        return null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            boolean back = getOnBackCallback().onBack();
            if (back) {
                finish();
                return;
            }
            return;
        }
        super.onBackPressed();
    }
}
